﻿@{
    ViewBag.Title = "Búsqueda por mapa";
}
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

@*Validación de usuario, para que no se pueda acceder a la página sin estar logueado *@
@if(@User.Identity.Name=="")
{
    Layout = null;
    <div style="width:800px; font-size:2em; height:500px; text-align:center; margin-top:100px" class="division">
    @Html.ActionLink("Ingrese como usuario válido...", "InicioSesion", "Cuenta")
    </div>
}
else
{
<h2>Búsqueda por mapa</h2>  
<div id="map" style="width: 800px; height: 550px; margin: 0 auto 0 auto;" ></div>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
}
<script type="text/javascript">
    var map;
    var array = [];
    var zoomLevel;

    $(document).ready(function () {
        var centerPoint = new google.maps.LatLng(-31, -69);
        var options = {
            zoom: 5,
            minZoom: 5,
            streetViewControl: false,
            center: centerPoint,
            mapTypeId: google.maps.MapTypeId.HYBRID
        };

        map = new google.maps.Map($("#map")[0], options);
        var infoWindow = new google.maps.InfoWindow();

        AddPointsToMap('@Url.Action("GetRegiones", "Region")');

        google.maps.event.addListener(map, 'zoom_changed', function () {
            zoomLevel = map.getZoom();

            switch (zoomLevel) {
                case 5:
                    AddPointsToMap('@Url.Action("GetRegiones", "Region")');
                    break;
                case 9:
                    AddPointsToMap('@Url.Action("GetPlantas", "Planta")');
                    break;
                case 17:
                    AddPointsToMap('@Url.Action("GetInstalaciones", "Instalacion")');
                    break;
            }
        });

        function AddPointsToMap(actionUrl) {
            for (i in array)
                array[i].setMap(null);

            $.ajax({
                type: "POST",
                url: actionUrl,
                datatype: "json",
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var point = new google.maps.LatLng(data[i].Latitud, data[i].Longitud);

                        var marker = new google.maps.Marker({
                            position: point,
                            map: map,
                            icon: '../Content/images/semaforo/verde.png',
                            title: data[i].Nombre,
                            html: data[i].Tipo + 'Instalación: ' + data[i].Nombre + '</br><a href="../' + data[i].Controller + '/' + data[i].Action + '/' + data[i].Parametro.toString() + '">Ir a instalación</a>'
                        });
                        array.push(marker);

                        if (data[i].NivelAlarma == "INVESTIGACION") {
                            marker.setAnimation(google.maps.Animation.BOUNCE);
                            marker.setIcon('../Content/images/semaforo/amarillo.png');
                        }
                        else 
                        if (data[i].NivelAlarma == "INTERVENCION") {
                            marker.setAnimation(google.maps.Animation.BOUNCE);
                            marker.setIcon('../Content/images/semaforo/rojo.png');
                        }



                        google.maps.event.addListener(marker, "click", function () {
                            if (this.html.substring(0, 3) == 'INS') {
                                infoWindow.setContent(this.html.substring(3));
                                infoWindow.open(map, this);
                            }
                            else {
                                var zoomLevel = map.getZoom();
                                var zoom;

                                if (zoomLevel <= 5)
                                    zoom = 9;
                                else if (zoomLevel <= 9)
                                    zoom = 17;
                                else if (zoomLevel < 17)
                                    zoom = 17;
                                else
                                    zoom = 5;

                                map.setCenter(this.getPosition());
                                map.setZoom(zoom);
                            }
                        });
                    }
                }
            });
        }
    });
</script>